<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class WatchListModel extends CI_Model
{
    public function __construct(){
        parent::__construct();
        $this->load->model('entities/watchlistentity');
        $this->load->model('entities/watchlistdetailsentity');  
    }
    
    public function getWatchLists($id = null)
    {
        $result = null;
        $this->db->select('id, name');
        if ($id != null) {
            $query = $this->db->get_where('tbl_watchlist', array('id' => $id));
            $result = $query->result('WatchListEntity');
            $result = $result[0];
        } else {
            $query = $this->db->get('tbl_watchlist');
            $result = $query->result('WatchListEntity');
        }
        return $result;
    }

    public function getWatchListsDetails($wlId)
    {
        $this->db->from('tbl_watchlist_details');
        $this->db->where(array('details_id' => $wlId));
        $query = $this->db->get();
        return $query->result('WatchListDetailsEntity');
    }

    public function loadWatchListDetail($wlId)
    {
        $this->db->select('symbol_id_a, symbol_id_b');
        $query = $this->db->get_where("tbl_watchlist_details", array('details_id' => $wlId));
        return $query->result('WatchListDetailsEntity');
    }

    public function insertWatchList($wlName, $arrWLD)
    {
        $wl = new WatchListEntity();
        $wl->setName($wlName);
        $this->db->trans_begin();
        $this->db->insert('tbl_watchlist', $wl);

        $this->db->select('id');
        $query = $this->db->get_where('tbl_watchlist', array('name' => $wlName), 1, 0)->
            row_array(0);
        $curId = $query['id'];
        foreach ($arrWLD as &$row) {
            $row->setDetailsId($curId);
            $row = (array )$row;
        }
        $this->db->insert_batch('tbl_watchlist_details', $arrWLD);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }

    public function updateWatchList($wlInfo, $arrWLD)
    {
        $this->db->trans_begin();
        $this->db->where('id', $wlInfo->getId());
        $this->db->update('tbl_watchlist', $wlInfo);

        $this->db->where('details_id', $wlInfo->getId());
        $query = $this->db->delete('tbl_watchlist_details');
        foreach ($arrWLD as &$row) {
            $row->setDetailsId($wlInfo->getId());
            $row = (array )$row;
        }
        $this->db->insert_batch('tbl_watchlist_details', $arrWLD);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }

    public function deleteWL($id)
    {
        $this->db->trans_begin();
        $this->db->delete('tbl_watchlist_details', array('details_id' => $id));
        $this->db->delete('tbl_watchlist', array('id' => $id));
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }
}
